package com.phenix.wanandroid.ui.main

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.Button
import androidx.compose.material.OutlinedButton
import androidx.compose.material.Switch
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import com.phenix.wanandroid.Constants
import com.phenix.wanandroid.viewmodel.MainViewModel
import com.tencent.mmkv.MMKV

@Composable
fun MainScreenHello(onContinueClicked: () -> Unit) {
    val viewModel: MainViewModel = viewModel()

    val k = "androidx"

    val expanded = remember { mutableStateOf(false) }

    var isDevelopment by remember {
        val isDevelop = MMKV.defaultMMKV().decodeBool(Constants.KEY_ENV_DEVELOP, true)
        mutableStateOf(isDevelop)
    }

    val hello = viewModel.hello.observeAsState()

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier
            .fillMaxHeight()
            .fillMaxWidth()
    ) {
        Text(text = "Hello ${hello.value}!")
        Button(onClick = { viewModel.getMavenPomList(k) }) {
            Text(text = "获取MavenPom列表数据")
        }
        OutlinedButton(
            onClick = {
                expanded.value = !expanded.value
                onContinueClicked()
            }
        ) {
            Text(if (expanded.value) "Show less" else "Show more")
        }

        Switch(checked = isDevelopment, onCheckedChange = {
            isDevelopment = it
            MMKV.defaultMMKV().encode(Constants.KEY_ENV_DEVELOP, it)
        })

    }
}